//
//  TopicImage.m
//  ZhihunewPaper
//
//  Created by 梅杰 on 16/7/5.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "TopicImage.h"
#import "Masonry.h"
@interface TopicImage ()


@end

@implementation TopicImage

- (instancetype) initWithFrame:(CGRect )frame Title : (NSString *)title {
    
    if (self = [super init]) {
        self.frame = frame;
        self.title = title;
        //根据title的内容进行适配Label的尺寸
        UILabel *aLabel = [[UILabel alloc] init];
        _TitleLabel = aLabel;
        _TitleLabel.textColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] init];
        _ImageView = imageView;
//        _ImageView.backgroundColor = [UIColor redColor];
//        _TitleLabel.backgroundColor = [UIColor blueColor];
        self.TitleLabel.text = title;
        
        [self addSubview:_ImageView];
        [self addSubview:_TitleLabel];
        
        __weak typeof(self) __weakMe = self;
        CGFloat titleLabelHeight = [self sizeForText:self.TitleLabel.text].size.height;

        [_TitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(__weakMe).offset(-Margin);
            make.height.mas_equalTo(titleLabelHeight + 10);
            make.left.equalTo(__weakMe).mas_offset(Margin);
            make.bottom.equalTo(__weakMe).mas_offset(-Margin-37);
        }];
//        _TitleLabel.frame = CGRectMake(0, 0, 100, 200);

        [_ImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(__weakMe);
        }];
           }

    return self;
}
- (void)setTitle:(NSString *)title{
    _title = title;
    self.TitleLabel.text = title;
}
- (void)setImage:(UIImage *)image{
    _image = image;
    self.ImageView.image = image;
}

- (CGRect) sizeForText : (NSString *)text{
    UIFont *font = [UIFont fontWithName:@"ArialHebrew-Bold" size:21.0];

    _TitleLabel.font = font;
    _TitleLabel.numberOfLines = 0;
    _TitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGFloat width = self.frame.size.width - Margin * 2;
    //根据字体尺寸得到尺寸
    NSDictionary *attributes = @{NSFontAttributeName : font};
//    CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
    CGRect retRect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return retRect;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
